<template>
  <div>
    <el-button type="text" @click="save">添加科室</el-button><br />
    <el-col :span="5">
      <el-input v-model="deptName" placeholder="请输入科室名称"></el-input>
    </el-col>
    <el-button type="text" @click="search">查询</el-button>

    <el-dialog title="人员名单" :visible.sync="dialogTableVisible">
      <el-table :data="gridData">
        <el-table-column
          property="realName"
          label="姓名"
          width="150"
        ></el-table-column>
        <el-table-column
          property="userAge"
          label="年龄"
          width="200"
        ></el-table-column>
        <el-table-column
          property="userPhone"
          label="电话"
          width="200"
        ></el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog title="添加科室" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="科室名称" :label-width="formLabelWidth">
          <el-input v-model="form.deptName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" :label-width="formLabelWidth">
          <div class="block">
            <el-cascader
              v-model="form.parentId"
              :options="tableData"
              :show-all-levels="false"
              :props="{
                expandTrigger: 'hover',
                value: 'deptId',
                label: 'deptName',
                children: 'children',
                emitPath: false,
              }"
              @change="handleChange"
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitform">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      :data="addtableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="deptId"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="deptId" label="科室编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="deptName" label="科室名称" sortable width="180">
      </el-table-column>
      <el-table-column label="操作" sortable width="180">
        <template slot-scope="scope">
          <el-button type="danger" @click="del(scope.row)">删除</el-button>
          <el-button type="info" @click="update(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getlist,
  dele,
  update,
  save,
  search,
  selectAll,
} from "@/api/dept/dept.js";
export default {
  data() {
    return {
      gridData: [],
      dialogTableVisible: false,
      tableData: [],
      currentPage: 1,
      pageNumber: 1,
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "120px",
      deptName: "",
      addtableData: [],
    };
  },
  methods: {
    select() {
      selectAll().then((r) => {
        this.tableData = r.data;
      });
    },
    search() {
      search({
        deptName: this.deptName,
        pageNumber: this.pageNumber,
        pageSize: this.pageSize,
      }).then((r) => {
        if ((r.code = 200)) {
          this.gridData = r.data.list;
          this.dialogTableVisible = true;
        }
      });
    },
    getlist() {
      getlist({ pageNumber: this.pageNumber, pageSize: this.pageSize }).then(
        (r) => {
          this.total = r.data.total;
          this.addtableData = r.data.list;
        }
      );
    },
    handleChange() {},
    save() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    submitform() {
      if (this.form.deptId == null) {
        save(this.form).then((r) => {
          this.getlist();
          this.$message.success("添加成功");
        });
        this.dialogFormVisible = false;
        return;
      }
      update(this.form).then((r) => {
        this.$message.success("修改成功");
        this.getlist();
        this.dialogFormVisible = false;
      });
    },
    del(row) {
      if (window.confirm("确认删除吗？")) {
        dele({ deptId: row.deptId }).then((r) => {
          if (r.code == 200) {
            this.getlist();
            this.$message.success("删除成功");
          } else {
            this.$message.error("删除失败");
          }
        });
      }
    },
    update(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      if (this.deptName == null) {
        this.getlist();
        return;
      }
      this.search();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      if (this.deptName == null) {
        this.getlist();
        return;
      }
      this.search();
    },
  },
  mounted() {
    this.getlist();
    this.select();
  },
};
</script>

<style></style>